package canss.webservice.controls;


import org.apache.beehive.controls.api.bean.ControlImplementation;
import org.apache.beehive.controls.api.bean.Control;

import canss.encoder.DBStringEncoder;
import canss.webservice.model.PID;


import weblogic.jdbc.SQLExceptionWrapper;

import java.io.Serializable;

@ControlImplementation
public class PIDControlImpl implements PIDControl, Serializable
{
	private static final long serialVersionUID = -2538730542000149841L;

	@Control
	private BusinessSystemDB businesssystemDB;

	public Boolean createPersonID(String _pid) throws SQLExceptionWrapper
	{
		String tmp = DBStringEncoder.Server2DB(_pid);
		if (tmp == null)
			return false;
		int ret = businesssystemDB.createPersonID(tmp);
		return ret>0;
	}

	public Boolean isDuplicatedPID(String _pid) throws SQLExceptionWrapper
	{
		PID[] personlist = businesssystemDB.findPersonID(DBStringEncoder
				.Server2DB(_pid));
		if (personlist!=null && personlist.length > 0)
			return true;
		return false;
	}

	public int getNumberofPersons() throws SQLExceptionWrapper
	{
		return businesssystemDB.getNumberofPersons();
	}
	public String[] getPIDWithin(int _numbers) throws SQLExceptionWrapper
	{
		PID[] personlist = businesssystemDB.getPersonsWithin(_numbers);
		if(personlist==null)
			return null;
		String[] ret = new String[personlist.length];
		for (int i = 0; i < personlist.length; i++)
			ret[i] = DBStringEncoder.DB2Server(personlist[i].getPID());
		return ret;
	}
}